package com.wsd.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsd.bean.ProductComment;
import com.wsd.bean.ProductOrder;
import com.wsd.dao.ProductCommentDaoInterface;

/**
 * A data access object (DAO) providing persistence and search support for
 * ProductOrder entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.wsd.bean.ProductOrder
 * @author MyEclipse Persistence Tools
 */

public class ProductCommentDAO extends HibernateDaoSupport implements ProductCommentDaoInterface {
	private static final Log log = LogFactory.getLog(ProductCommentDAO.class);

	protected void initDao() {
		// do nothing
	}

	public void save(ProductComment transientInstance) {
		log.debug("saving ProductComment instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ProductComment persistentInstance) {
		log.debug("deleting ProductComment instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ProductComment findById(java.lang.Integer id) {
		log.debug("getting ProductComment instance with id: " + id);
		try {
			ProductComment instance = (ProductComment) getHibernateTemplate().get(
					"com.wsd.bean.ProductComment", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductOrderDaoInterface#findByStateUid(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	public List<ProductComment> findByStateUid(Integer uid) {
		log.debug("finding ProductOrder by orderstate and userid instances");
		try {
			String queryString = "from ProductComment where PCState = 0 and users.userid = ? order by PCId desc";
			Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
			query.setInteger(0, uid);
			return query.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List<ProductComment> findByExample(ProductComment comment, int start, int limit) {
		String sql = "";
		if(comment.getPCState() == null){
			sql = "from ProductComment where products.PId = :pid order by PCTime desc";
		}else{
			sql = "from ProductComment where products.PId = :pid and PCState = :state order by PCTime desc";
		}
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		if(comment.getPCState() != null){
			query.setBoolean("state", comment.getPCState());
		}
		query.setInteger("pid", comment.getProducts().getPId());
		query.setMaxResults(limit);
		query.setFirstResult(start);
		return query.list();
	}
	
	public int findByExampleCount(ProductComment comment) {
		String sql = "";
		if(comment.getPCState() == null){
			sql = "select count(*) from ProductComment where products.PId = :pid order by PCId desc";
		}else{
			sql = "select count(*) from ProductComment where products.PId = :pid and PCState = :state order by PCId desc";
		}
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		if(comment.getPCState() != null){
			query.setBoolean("state", comment.getPCState());
		}
		query.setInteger("pid", comment.getProducts().getPId());
		Long count = (Long) query.uniqueResult();
		return count.intValue();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductOrderDaoInterface#updateState(int, boolean)
	 */
	public void updateState(int id, boolean state) {
		String sql = "update ProductComment set PCState = :state where PCId = :id";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setInteger("id", id);
		query.setBoolean("state", state);
		query.executeUpdate();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductOrderDaoInterface#history(java.util.Date, java.util.Date, int, int)
	 */
	public List<ProductComment> history(Date startDate, Date endDate, int start, int limit) {
		String sql = "from ProductComment where PCTime between :startDate and :endDate";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setDate("startDate", startDate);
		query.setDate("endDate", endDate);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		return query.list();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductOrderDaoInterface#historyCount(java.util.Date, java.util.Date)
	 */
	public int historyCount(Date startDate, Date endDate) {
		String sql = "select count(*) from ProductComment where OTime between :startDate and :endDate";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setDate("startDate", startDate);
		query.setDate("endDate", endDate);
		
		Long count = (Long) query.uniqueResult();
		return count.intValue();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductCommentDaoInterface#deleteById(int)
	 */
	public void deleteById(int pid) {
		String sql = "delete from ProductComment where PCId =:pid";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setInteger("pid", pid);
		query.executeUpdate();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductCommentDaoInterface#findAll(int, int)
	 */
	public List<ProductComment> findAll(int start, int limit) {
		String sql = "from ProductComment p where PCState = false order by p.PCId desc";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		return query.list();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductCommentDaoInterface#findAllCount()
	 */
	public int findAllCount() {
		String sql = "select count(*) from ProductComment";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		Long count = (Long) query.uniqueResult();
		return count.intValue();
	}

}